﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Generator : MonoBehaviour {


	[Serializable]
	public class StarSystems
	{
		public GameObject Sys;
		public int MaxCounts;
		public int MinCounts;
		public float MaxRange;
		public float MinRange;
	}
	[SerializeField]
	public StarSystems[] Stars;

	[HideInInspector]
	public List<GameObject> Total;
	[HideInInspector]
	Dictionary<string, int> TotalType;  

	void GenerateCargo()//预存进池
	{
		Total = new List<GameObject> ();
		TotalType = new Dictionary<string, int> ();
		int Count = 0;
		for (int i = 1; i < Stars.Length; i++) 
		{
			for(int j=0;j<UnityEngine.Random.Range(Stars[i].MinCounts,Stars[i].MaxCounts);j++)
			{
				Total.Add(Instantiate(Stars[i].Sys));	
				TotalType.Add (Count.ToString(),i);
				Count++;
			}
		}
		for(int i=1;i<=ControlCenter.Instance.Players.Count;i++)
		{
			GameObject clone;
			clone = Instantiate (Stars[0].Sys);
			clone.GetComponent<StarSystem>().Owner=i;
			Total.Add (clone);
			TotalType.Add (Count.ToString(),0);
			Count++;
		}

	}
		
	public float Rad;//范围

	public float PlayerRad;
	//Rad*5 /ControlCenter.Instance.PlayersPrefab.Length;
	public float Max;//最小距离
	public float Min;//最大距离

	void Start () {
		StartGenerate ();
	}
	void StartGenerate()
	{		
		GenerateCargo ();
		RandomSystem ();
	}
	void ReStartAll()
	{
		ClearAll ();
		StartGenerate ();
	}
	void ClearAll()
	{
		foreach (GameObject obj in Total) 
		{
			Destroy (obj);
		}
		Total.Clear();
	}
	void RandomSystem()
	{
		int BreakAllNum=0;

		for (int i = 0; i < Total.Count; i++) 
		{	
			if (BreakAllNum > 1000)//重算保护机制
				ReStartAll ();
			Total[i].transform.position = new Vector2(UnityEngine.Random.Range (-Stars[TotalType[i.ToString()]].MaxRange, Stars[TotalType[i.ToString()]].MaxRange), UnityEngine.Random.Range (-Stars[TotalType[i.ToString()]].MaxRange, Stars[TotalType[i.ToString()]].MaxRange));
			Total[i].name = "system_"+i.ToString();
			if (!GenerateJudge(i))
			{
				BreakAllNum++;
				i--;
			}

		}
	}
	bool GenerateJudge(int i)
	{
		if (ClosetSystem (Total[i]) > Max || ClosetSystem (Total[i]) < Min)
			return false;
		if (Total[i].transform.position.magnitude>Stars[TotalType[i.ToString()]].MaxRange || Total[i].transform.position.magnitude<Stars[TotalType[i.ToString()]].MinRange)
			return false;
		if (PlayerDist (Total[i]) < PlayerRad)
			return false;
		return true;
	}
	float ClosetSystem(GameObject go)
	{
		float dis = Mathf.Infinity;
		GameObject[] Systems;
		Systems = GameObject.FindGameObjectsWithTag ("systems");
		foreach (GameObject sys in Systems) 
		{
			if (sys == go)
				continue;
			float DisNow = (sys.transform.position - go.transform.position).magnitude;
			if (DisNow < dis)
				dis = DisNow;
		}
		return dis;
	}
	float PlayerDist(GameObject go)
	{
		if (go.GetComponent<StarSystem> ().Owner == 0)
			return Mathf.Infinity;
		float dis = Mathf.Infinity;
		GameObject[] Systems;
		Systems = GameObject.FindGameObjectsWithTag ("systems");
		foreach (GameObject sys in Systems) 
		{
			if (sys == go)
				continue;
			float DisNow = (sys.transform.position - go.transform.position).magnitude;
			if (DisNow < dis && sys.GetComponent<StarSystem>().Owner!= 0)
				dis = DisNow;
		}
		return dis;
	}
}
